/**
 * Copyright &copy; com.smooth.common
 */
package com.smooth.site.sx.mag.biz.offer.service;

import java.util.List;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.smooth.common.site.common.PageDb;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.smooth.dao.gen.bean.BizOffer;
import com.smooth.dao.gen.bean.BizOfferExample;
import com.smooth.dao.gen.BizOfferMapper;
import com.smooth.common.site.common.enums.DelFlagEnums;
import com.smooth.common.site.common.persistence.Page;
import com.smooth.common.site.common.utils.IdGenerater;

/**
 * offerService
 * @author bm
 * @version 2019-02-25
 */
@Service
@Transactional(readOnly = true)
public class BizOfferService{
	Logger log = LoggerFactory.getLogger(BizOfferService.class);
	@Autowired
	BizOfferMapper bizOfferMapper;
	@Autowired
	IdGenerater idGenerater;
	
	public BizOffer get(String no) {
		return bizOfferMapper.selectByPrimaryKey(no);
	}
	
	public List<BizOffer> findList(BizOffer bizOffer) {
		BizOfferExample bizOfferExample = new BizOfferExample();
		BizOfferExample.Criteria criteria = bizOfferExample.createCriteria();
		
		return bizOfferMapper.selectByExample(bizOfferExample);
	}
	
	public Page<BizOffer> findPage(Page<BizOffer> page, BizOffer bizOffer) {
		BizOfferExample bizOfferExample = new BizOfferExample();
		BizOfferExample.Criteria criteria = bizOfferExample.createCriteria();
		
		bizOfferExample.setPage(page);
		
		List<BizOffer> dlist = bizOfferMapper.selectByExample(bizOfferExample);
		page.setList(dlist);
		page.setCount(bizOfferMapper.countByExample(bizOfferExample));
		
		return page;
	}
	
	@Transactional(readOnly = false)
	public BizOffer save(BizOffer bizOffer) {
		if(StringUtils.isNotEmpty(bizOffer.getId())&&get(bizOffer.getId())!=null) {
			int optNum = bizOfferMapper.updateByPrimaryKeySelective(bizOffer);
			if(optNum==1) {
				log.info("更新 student 数据【"+bizOffer.toString()+"】成功");
			}else {
				log.error("更新 student 数据【"+bizOffer.toString()+"】失败");
			}
			return bizOfferMapper.selectByPrimaryKey(bizOffer.getId());
		}else {
			Date date = new Date();
			bizOffer.setCreateDate(date);
			bizOffer.setId(idGenerater.genClsNo("bizOffer"));
			bizOffer.setUpdateDate(date);
			int optNum =  bizOfferMapper.insertSelective(bizOffer);
			if(optNum==1) {
				log.info("插入 student 数据【"+bizOffer.toString()+"】成功");
			}else {
				log.error("插入 student 数据【"+bizOffer.toString()+"】失败");
			}
			return bizOffer;
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(BizOffer bizOffer) {
		bizOfferMapper.deleteByPrimaryKey(bizOffer.getId());
	}
	
}